package by.imotaeva.printing_services.services.impl;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import by.imotaeva.printing_services.dataaccess.BindingOrderDao;
import by.imotaeva.printing_services.dataaccess.PrintingProductOrderDao;
import by.imotaeva.printing_services.datamodel.BindingOrder;
import by.imotaeva.printing_services.datamodel.PrintingProductOrder;
import by.imotaeva.printing_services.services.BindingOrderService;

@Service
public class BindingOrderServiceImpl implements BindingOrderService {
	private static final Logger LOGGER = LoggerFactory
			.getLogger(BindingOrderServiceImpl.class);

	@Inject
	private PrintingProductOrderDao printingDao;
	@Inject
	private BindingOrderDao dao;

	@PostConstruct
	private void init() {
		// this method will be called by Spring after bean instantiation. Can be
		// used for any initialization process.
		LOGGER.info("Instance of BindingOrderService is created. Class is: {}",
				getClass().getName());
	}

	@Override
	public void createNewBindingOrder(
			PrintingProductOrder printingProductOrder, BindingOrder bindingOrder) {
		Validate.isTrue(
				bindingOrder.getId() == null,
				"This method should be called for 'not saved yet' bindingOrder only. Use UPDATE instead");
		printingDao.insert(printingProductOrder);
		bindingOrder.setPrintingProductOrder(printingProductOrder);
		dao.insert(bindingOrder);

	}

	@Override
	public BindingOrder get(Long id) {
		return dao.getById(id);

	}

	@Override
	public void update(BindingOrder entity) {
		dao.update(entity);

	}

	@Override
	public void removeBindingOrder(Long id) {
		dao.delete(id);
		printingDao.delete(id);
	}

	@Override
	public List<BindingOrder> getAllBindingOrders() {
		return dao.getAll();

	}

	@Override
	public void deleteAllBindingOrders() {
		LOGGER.debug("Remove all binding orders");
		dao.deleteAll();
	}

	@Override
	public List<Long> getBindingOrderIds() {

		return dao.getBindingOrderIds();
	}

	@Override
	public Long getCount() {
		return dao.getCount();
	}

	@Override
	public BindingOrder getFullBindingOrder(Long id) {
		return dao.getFullBindingOrder(id);
	}

}
